package writer

import (
	"encoding/json"
	"github.com/prometheus/common/log"
	"ihub/agent/pkg/datas"
	"ihub/agent/pkg/event"
	"io"
	"strings"
)

type StdOutWrapper struct {
	W io.Writer
	Eds string
}
func (o *StdOutWrapper) Write(p []byte) (n int, err error){
	logData:=&datas.NgLog{}
	err2:=json.Unmarshal(p,logData)
	if err2 !=nil {
		log.Error(err2)
	}else if logData.Status==404 && logData.Method=="GET"{
		off:=strings.Index(logData.Uri,"/manifests/")
		isV2:=strings.HasPrefix(logData.Uri,"/");

		if isV2 && off > 0{
			log.Error("----->  ",logData.Uri)
			str:=strings.TrimPrefix(logData.Uri,"/v2")
			str=strings.ReplaceAll(str,"/manifests/",":")
			go event.Fire(logData.Host,str,o.Eds);
		}
	}
	return o.W.Write(p)
}